<template>
  <div>
    <header>
      <div>
        <p>累计想看</p>
        <p>405780</p>
      </div>
      <div>
        <p>女姓想看</p>
        <p>46.2%</p>
      </div>
      <div>
        <p>男性想看</p>
        <p>53.8%</p>
      </div>
    </header>
    <div class="add">
      <van-cell title="选择日期区间" :value="date" @click="show = true" />
      <van-calendar v-model="show" type="range" @confirm="onConfirm" :round="false" position="right" />
      <main id="main1"></main>
    </div>
    <main id="main2"></main>
    <div class="w">
      <p>想看分析</p>
      <main id="main3"></main>
    </div>
  </div>
</template>

<script>
import getLink from '@/api/getLink.js';
export default {
  data() {
    return {
      main1Data: [],
      date: '',
      show: false,
    };
  },
  created() {
    getLink('/api/analyse/want').then(ok => {
      this.main1Data = ok.data.map(ele => {
        return ele.number;
      });
      let myChart = this.$echarts.init(document.getElementById('main1'));
      // 绘制图表
      myChart.setOption({
        title: {
          text: '想看新增',
          textStyle: {
            fontWeight: 'lighter',
            color: 'black',
          },
          top: 10,
        },
        legend: {
          show: true,
          right: 20,
          top: 20,
        },
        tooltip: {
          triggerOn: 'mousemove',
          enterable: true,
        },
        xAxis: {
          data: ['06/30', '07/01', '07/02', '07/03', '07/04', '07/05', '07/06', '07/07', '07/08', '07/09', '07/10', '07/11', '07/12', '07/13'],
          type: 'category',
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          position: 'left',
          offset: -5,
        },
        series: {
          name: '新增想看',
          type: 'line',
          data: this.main1Data,
          legendHoverLink: true,
          itemStyle: {
            normal: {
              color: 'orange',
              lineStyle: {
                color: 'orange',
                borderColor: 'orange',
              },
            },
          },
          emphasis: { scale: true, focus: 'self' },
        },
        toolbox: { show: true },
      });
    });
  },
  mounted() {
    this.ec1();
    this.ec2();
  },
  methods: {
    formatDate(date) {
      return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirm(date) {
      const [start, end] = date;
      this.show = false;
      this.date = `${this.formatDate(start)} - ${this.formatDate(end)}`;
      this.main1Data.reverse();
      let myChart = this.$echarts.init(document.getElementById('main1'));
      // 绘制图表
      myChart.setOption({
        title: {
          text: '想看新增',
          textStyle: {
            fontWeight: 'lighter',
            color: 'black',
          },
          top: 10,
        },
        legend: {
          show: true,
          right: 20,
          top: 20,
        },
        tooltip: {
          triggerOn: 'mousemove',
          enterable: true,
        },
        xAxis: {
          data: ['06/30', '07/01', '07/02', '07/03', '07/04', '07/05', '07/06', '07/07', '07/08', '07/09', '07/10', '07/11', '07/12', '07/13'],
          type: 'category',
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          position: 'left',
          offset: -5,
        },
        series: {
          name: '新增想看',
          type: 'line',
          data: this.main1Data.slice(Math.random() * 10, Math.random() * 10 + 15),
          legendHoverLink: true,
          itemStyle: {
            normal: {
              color: 'orange',
              lineStyle: {
                color: 'orange',
                borderColor: 'orange',
              },
            },
          },
          emphasis: { scale: true, focus: 'self' },
        },
        toolbox: { show: true },
      });
    },
    ec1() {
      let myChart1 = this.$echarts.init(document.getElementById('main2'));
      myChart1.setOption({
        title: {
          text: '想看用户',
          textStyle: {
            fontWeight: 'lighter',
            color: 'black',
          },
          top: 10,
        },
        grid: {
          left: 50,
        },
        tooltip: {
          triggerOn: 'mousemove',
          enterable: true,
        },
        xAxis: {},
        yAxis: {
          scale: true,
        },
        series: [
          {
            name: '1990',
            type: 'effectScatter',
            data: [
              [28604, 77, 17096869, 'Australia', 1990],
              [31163, 77.4, 27662440, 'Canada', 1990],
              [1516, 68, 1154605773, 'China', 1990],
              [13670, 74.7, 10582082, 'Cuba', 1990],
              [28599, 75, 4986705, 'Finland', 1990],
              [29476, 77.1, 56943299, 'France', 1990],
              [31476, 75.4, 78958237, 'Germany', 1990],
              [28666, 78.1, 254830, 'Iceland', 1990],
              [1777, 57.7, 870601776, 'India', 1990],
              [29550, 79.1, 122249285, 'Japan', 1990],
              [2076, 67.9, 20194354, 'North Korea', 1990],
              [12087, 72, 42972254, 'South Korea', 1990],
              [24021, 75.4, 3397534, 'New Zealand', 1990],
              [43296, 76.8, 4240375, 'Norway', 1990],
              [10088, 70.8, 38195258, 'Poland', 1990],
              [19349, 69.6, 147568552, 'Russia', 1990],
              [2076, 67.9, 20194354, 'North Korea', 1990],
              [12087, 72, 42972254, 'South Korea', 1990],
              [24021, 75.4, 3397534, 'New Zealand', 1990],
              [43296, 76.8, 4240375, 'Norway', 1990],
              [10088, 70.8, 38195258, 'Poland', 1990],
              [19349, 69.6, 147568552, 'Russia', 1990],
              [10670, 67.3, 53994605, 'Turkey', 1990],
              [26424, 75.7, 57110117, 'United Kingdom', 1990],
              [37062, 75.4, 252847810, 'United States', 1990],
            ],
            symbolSize: function(data) {
              return Math.sqrt(data[2]) / 5e2;
            },
          },
        ],
      });
    },
    ec2() {
      let myChart1 = this.$echarts.init(document.getElementById('main3'));
      myChart1.setOption({
        legend: {
          orient: 'vertical',
          left: 'left',
          data: ['0-10', '10-20', '20-30', '30-40', '40-50'],
        },
        series: [
          {
            type: 'pie',
            data: [
              {
                value: 335,
                name: '0-10',
              },
              {
                value: 310,
                name: '10-20',
              },
              {
                value: 234,
                name: '20-30',
              },
              {
                value: 135,
                name: '30-40',
              },
              {
                value: 1548,
                name: '40-50',
              },
            ],
          },
        ],
      });
    },
  },
};
</script>

<style lang="less" scoped>
header {
  display: flex;
  justify-content: space-around;
  background-color: white;
  height: 1.5rem;
  align-items: center;
  div {
    text-align: center;
    p {
      font-size: 0.25rem;
      &:last-of-type {
        font-size: 0.35rem;
      }
    }
  }
}
.add {
  margin-top: 0.2rem;
  background-color: white;
  box-sizing: border-box;
  padding: 0 0.2rem;
  #main1 {
    width: 100%;
    height: 5rem;
    p {
      margin: 0.2rem 0;
      font-size: 0.35rem;
    }
  }
}
#main2 {
  margin-top: 0.2rem;
  width: 100%;
  height: 5rem;
  background-color: white;
  box-sizing: border-box;
  padding: 0 0.2rem;
}
.w {
  margin-top: 0.2rem;
  width: 100%;
  height: 5rem;
  background-color: white;
  p {
    padding: 0.1rem 0.3rem;
    font-size: 0.35rem;
  }
  #main3 {
    box-sizing: border-box;
    padding: 0 0.2rem;
    width: 100%;
    height: 5rem;
  }
}
</style>
